package adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;

import java.io.Serializable;
import java.util.List;

import bean.LunTanJiYanBean;
import jiyun.com.yiyanmeng.LunTanJinYanDetailTopActivity;
import jiyun.com.yiyanmeng.R;

/**
 * Created by zhang'lin'lin on 2019/11/7.
 */

public class LunTanJiYanAdapter extends RecyclerView.Adapter {
    private List<LunTanJiYanBean.InfoBean.TopBean> top;
    private List <LunTanJiYanBean.InfoBean.ArticleBean> bottom;//经验论坛
    private Context context;

    public LunTanJiYanAdapter(List <LunTanJiYanBean.InfoBean.TopBean> top, List <LunTanJiYanBean.InfoBean.ArticleBean> bottom, Context context) {
        this.top = top;
        this.bottom = bottom;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==0){
            View view = LayoutInflater.from(context).inflate(R.layout.item_luntanjiyan_top, parent, false);
            return new TopVH(view);
        }else{
            View view = LayoutInflater.from(context).inflate(R.layout.item_luntanjiyan_bottom, parent, false);
            return new BottVH(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if (position==0){
            TopVH topVH= (TopVH) holder;
            topVH.tv1.setText(top.get(position).getTitle());
            topVH.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                       if (monItemClick!=null){
                           monItemClick.onClick(position);
                       }
                }
            });
        }else{
            BottVH bottVH= (BottVH) holder;
            bottVH.tv1.setText(bottom.get(position).getAuthName());
            bottVH.tv2.setText(bottom.get(position).getAddtime());
            bottVH.tv3.setText(bottom.get(position).getTitle());
            bottVH.tv4.setText(bottom.get(position).getContent());
            bottVH.tv5.setText(bottom.get(position).getPing_num());
            RequestOptions options = new RequestOptions()
                    .circleCropTransform()
                    .error(R.drawable.default_ava) //加载失败图片
                    .priority(Priority.HIGH); //优先级
//                    .transform(new GlideRoundTransform(5)); //圆角
            Glide.with(context).load(bottom.get(position).getAuthImg()).apply(options).into(bottVH.iv1);
        }

    }

    public interface onItemClick{
        void onClick(int position);
    }
    private onItemClick monItemClick;

    public void setMonItemClick(onItemClick monItemClick) {
        this.monItemClick = monItemClick;
    }

    @Override
    public int getItemCount() {
        return bottom.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return 0;
        }else{
            return 1;
        }
    }

    class TopVH extends RecyclerView.ViewHolder{

        private final TextView tv1;

        public TopVH(View itemView) {
            super(itemView);
            tv1 = itemView.findViewById(R.id.tv1);

        }
    }

    class BottVH extends RecyclerView.ViewHolder{

        private final ImageView iv1;
        private final ImageView iv2;
        private final TextView tv1;
        private final TextView tv2;
        private final TextView tv3;
        private final TextView tv4;
        private final TextView tv5;

        public BottVH(View itemView) {
            super(itemView);
            iv1 = itemView.findViewById(R.id.iv1);
            iv2 = itemView.findViewById(R.id.iv2);
            tv1 = itemView.findViewById(R.id.tv1);
            tv2 = itemView.findViewById(R.id.tv2);
            tv3 = itemView.findViewById(R.id.tv3);
            tv4 = itemView.findViewById(R.id.tv4);
            tv5 = itemView.findViewById(R.id.tv5);
        }
    }

}
